package com.ubs.ejb;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.annotation.ejb.LocalBinding;

import com.ubs.common.ServerToWebAdaper;
import com.ubs.dom.Account;
import com.ubs.persistence.PersistenceConstants;
import com.ubs.persistence.entity.AccountEntity;
import com.ubs.scurity.UbsSecuityException;

/**
 * Session Bean implementation class AccountControllerBean
 */
@Stateful
@LocalBinding(jndiBinding="ubs/AccountController")
public class AccountController implements AccountControllerLocal {

	@PersistenceContext(name="ubs-jpa")
	private transient EntityManager em;
	
	private boolean diagnosticMode;
    /**
     * Default constructor. 
     */
    public AccountController() {
    }
    
    @Override
    public boolean doesLoginExist(String login) {
    	
    	Integer count = (Integer) em.createNamedQuery("AccountEntity.isLoginExists")
    		.setParameter("login", login)
    		.getSingleResult();
    	
    	return count > 0;
    }

	@Override
	public void registerAccount(Account account) {
		AccountEntity entity = new AccountEntity();
		entity.setLogin(account.getLogin());
		entity.setFirstName(account.getFirstName());
		entity.setLastName(account.getLastName());
		entity.setPhoneNumber(account.getPhoneNumber());
		entity.setEmail(account.getEmail());
		entity.setPassword(account.getPasswordMD5());
		entity.setBalance(0);
		entity.setEnabled(PersistenceConstants.ACCOUNT_ENABLED_VALUE);
		entity.setCreatedDate(new Date(System.currentTimeMillis()));
		entity.setTestAccount(diagnosticMode ? 
				PersistenceConstants.TEST_ACCOUNT_VALUE : PersistenceConstants.NON_TEST_ACCOUNT_VALUE);
		
		em.persist(entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Account loginAccount(String login, String passwordMD5) {
	
		List<com.ubs.persistence.entity.AccountEntity> accounts = 
			(List<com.ubs.persistence.entity.AccountEntity>) em.createNamedQuery("AccountEntity.selectByLoginPassword")
			.setParameter("login", login)
			.setParameter("password", passwordMD5)
			.getResultList();
		
			
		return accounts.size() > 0 ? ServerToWebAdaper.getAccount(accounts.get(0)) : null;
	}

	@Override
	public void updateProfile(Account account) throws UbsSecuityException {
	
		if (account == null) {
			throw new UbsSecuityException("updateProfile: account == null");
		}
		
		Account managedAccount = em.find(Account.class, account.getId());
		if (managedAccount == null) {
			throw new UbsSecuityException(
				"updateProfile: managedAccount == null, account.getId() = " + account.getId());
		}
		
		managedAccount.setFirstName(account.getFirstName());
		managedAccount.setLastName(account.getLastName());
		managedAccount.setPhoneNumber(account.getPhoneNumber());
		managedAccount.setPasswordMD5(account.getPasswordMD5());
	}
	
	/**
	 * {@link AccountControllerLocal#updateBalance(Long, double)}
	 */
	//TODO: add security annotation for this method
	@Override
	public void updateBalance(Long accountId, double deltaSum) throws UbsSecuityException {
		if(accountId == null || accountId == 0) {
			throw new UbsSecuityException("updateBalance: accountId = " + accountId);
		}
		
		em.createNamedQuery("AccountEntity.updateBalance")
			.setParameter("accountId", accountId)
			.setParameter("delta", deltaSum)
			.executeUpdate();
		
	}

	@Override
	public void setDiagnosticMode(boolean enabled) {
		diagnosticMode = enabled;
	}

}
